<?php

class Cookies implements ArrayAccess, Iterator
{
    private $deftime;
    private $position = 0;
    private $keys = null;
    private $keyscount = 0;

    function SetDefTime($deftime)
    {
         $this->deftime = $deftime;
    }

    function __construct($deftime=0)
    {
        if((int)$deftime==0)
            $this->SetDefTime(60*60*24*7);
        else
            $this->SetDefTime($deftime);
    }

    function offsetGet($name)
    {
        return isset($_COOKIE[$name]) ? $_COOKIE[$name] : null;
    }

    function offsetSet($name,$val)
    {
        setcookie($name, $val, time() + $this->deftime, '/', '', false);
        $_COOKIE[$name] = $val;
    }

    function offsetExists($name)
    {
        return isset($_COOKIE[$name]);
    }

    function offsetUnset($name)
    {
        unset($_COOKIE[$name]);
        setcookie($name, '', time() - $this->deftime, '/', '', false);
    }

    function Set($name,$val, $time)
    {
        setcookie($name, $val, time() + $time, '/', '', false);
        $_COOKIE[$name] = $val;
    }

    function rewind()
    {
        $this->position = 0;
        if(! $this->keys)
        {
            $this->keys = array_keys($_COOKIE);
            $this->keyscount = count($this->keys);
        }
    }

    function current()
    {
        return $_COOKIE[$this->keys[$this->position]];
    }

    function key()
    {
        return $this->keys[$this->position];
    }

    function next()
    {
        ++$this->position;
    }

    function valid()
    {
        return $this->position < $this->keyscount && isset($_COOKIE[$this->keys[$this->position]]);
    }
    
    function GetRaw()
    {
        return $_COOKIE;
    }

};

?>